﻿
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using SqlSugar;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using VueAdminService.Converter;
using VueAdminService.DTO;
using VueAdminService.Utils;
using VueAdminService.VO;

namespace VueAdminService.Controllers
{
	public class BaseController : ControllerBase
	{
		private readonly IServiceProvider _serviceProvider;
		private readonly LogUtil _log;
		protected readonly ISqlSugarClient _dbClient;
		protected readonly IMemoryCache _memoryCache;

		private readonly JsonSerializerOptions _options = new JsonSerializerOptions()
		{
			DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
			PropertyNameCaseInsensitive = true,
			//重新编码，解决中文乱码问题			
			Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,

		};
		public BaseController(IServiceProvider serviceProvider)
		{
			_serviceProvider = serviceProvider;
			_dbClient = serviceProvider.GetService<ISqlSugarClient>();
			_memoryCache = serviceProvider.GetService<IMemoryCache>();
			_log = serviceProvider.GetService<LogUtil>();

			//_options.Converters.Add(new DatetimeJsonConverter());
		}
		/// <summary>
		/// 成功状态返回操作结果
		/// </summary>	
		/// <param name="data">失败信息</param>
		/// <returns></returns>
		protected ResponseResult<T> SuccessResult<T>(T data)
		{
			return ResponseResult<T>.SuccessResult(data);
		}
		/// <summary>
		/// 失败状态返回操作结果
		/// </summary>		
		/// <param name="msg">失败信息</param>
		/// <param name="data">失败信息</param>
		/// <returns></returns>
		protected ResponseResult<T> FailedResult<T>(string msg, T data)
		{
			return ResponseResult<T>.FailedResult(msg, data);
		}
		/// <summary>
		/// 异常状态返回操作结果
		/// </summary>		
		/// <param name="msg">异常信息</param>
		/// <param name="data">失败信息</param>
		/// <returns></returns>
		protected ResponseResult<T> ErrorResult<T>(string msg, T data)
		{
			return ResponseResult<T>.ErrorResult(msg, data);
		}
		/// <summary>
		/// 返回自定义数据结果
		/// </summary>
		/// <param name="code">状态码</param>
		/// <param name="msg">异常信息</param>
		/// <param name="data">返回数据</param>		
		/// <returns></returns>
		protected ResponseResult<T> CustomResult<T>(ResponseCode code, string msg, T data)
		{
			return ResponseResult<T>.Result(code, msg, data);
		}
		/// <summary>
		/// 返回成功分页数据
		/// </summary>
		/// <param name="data"></param>
		/// <param name="rows">总记录数方便分页</param>
		/// <returns></returns>
		protected ResponsePageResult<T> SuccessPageResult<T>(T data, int rows = 0)
		{
			return new ResponsePageResult<T> { Code = ResponseCode.Success, Data = data, Rows = rows };
		}
		/// <summary>
		/// 返回失败分页数据
		/// </summary>
		/// <param name="msg"></param>		
		/// <returns></returns>
		protected ResponsePageResult<T> ErrorPageResult<T>(string msg)
		{
			return new ResponsePageResult<T> { Code = ResponseCode.Error, Msg = msg };
		}
		/// <summary>
		/// 返回自定义分页数据
		/// </summary>
		/// <param name="code"></param>
		/// <param name="msg"></param>
		/// <param name="data"></param>
		/// <param name="rows"></param>
		/// <returns></returns>
		public static ResponsePageResult<T> CustomPageResult<T>(ResponseCode code, string? msg, T data, int rows = 0)
		{
			return new ResponsePageResult<T> { Code = code, Msg = msg, Data = data, Rows = rows };
		}
		/// <summary>
		/// 记录异常日志
		/// </summary>
		/// <param name="exception"></param>
		/// <param name="msg"></param>
		protected void LogError(Exception? exception, string msg)
		{
			if (exception != null)
			{
				_log.Error(msg, exception);
			}
			else
			{
				_log.Error(msg);
			}
		}
		/// <summary>
		/// 设置缓存
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="key"></param>
		/// <param name="value"></param>
		/// <param name="miute">过期时间</param>
		protected void SetCache<T>(string key, T value, int miute = 2)
		{
			_memoryCache.Set<T>(key, value, TimeSpan.FromMinutes(2));
		}
		/// <summary>
		/// 获取缓存
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="key"></param>
		/// <returns></returns>
		protected T? GetCache<T>(string key)
		{
			return _memoryCache.Get<T>(key);
		}
	}
}
